﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace privateworking.DAO
{
    class Chukumingxi_yuancailiaoDao
    {
        //原材料查询全部
        public Bean.Chukuguanli_yuancailiaoBean chukumingxi_yuancailiaoDaoSelect(DateTime dateStart, DateTime dateEnd)
        {
            DB.DBbase dbbase = new DB.DBbase();
            Bean.Chukuguanli_yuancailiaoBean bean = new privateworking.Bean.Chukuguanli_yuancailiaoBean();
            string sql = "";
            sql = "select chuku_pinming, chuku_pinming,chuku_type,sum(chuku_zhongliang)  from chukuguanli where chuku_yanshou between '" + dateStart.ToString("yyyyMMdd") + "' and '" + dateEnd.ToString("yyyyMMdd") + "' group by chuku_pinming,chuku_type  order by chuku_pinming";
            if (dbbase.ExecuteSQL(sql.Trim()))
            {
                if (dbbase.rows.Length != 0)
                {
                    bean.Selectresult = dbbase.rows;
                    bean.Resultclo = dbbase.Col_Number;
                    bean.Resultrow = dbbase.Row_Number;
                }
                else
                {
                    bean.Selectresult = null;
                }
            }
            else
            {
                bean.Selectresult = null;
            }
            return bean;
        }

         //库存表更新
        public bool chukumingxi_yuancailiaoDaoUpdate(Bean.Chukuguanli_yuancailiaoBean bean)
        {
            DB.DBbase dbbase = new DB.DBbase();
            string sql = "";
           

            for (int i = 0; i < bean.Resultrow; i++)
            {
                string zhongliang_xuni_sql = "select zhongliangxuni from kucunguanli where pinming = '" + bean.Selectresult[i, 1] + "'";

                float zhongliang_xuni = 0;
                string chuku_no = "";
                
                if (dbbase.ExecuteSQL(zhongliang_xuni_sql.Trim()))
                {
                    zhongliang_xuni = float.Parse(dbbase.rows[0,0].ToString());
                     
                }

                sql = "update  kucunguanli set zhongliangshiji = '" + Convert.ToString(zhongliang_xuni + float.Parse(bean.Selectresult[i, 3].ToString())) + "' where pinming = '" + bean.Selectresult[i, 1] +"'";
                if (!dbbase.ExecuteSQL(sql.Trim()))
                {
                    return false;
                }
            }

            if (dbbase.command())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        //出库管理表更新
        public bool chukuguanli_yuancailiaoDaoUpdate(Bean.Chukuguanli_yuancailiaoBean bean, string startDay, string endDay)
        {
            Bean.Chukuguanli_yuancailiaoBean tempBean = new privateworking.Bean.Chukuguanli_yuancailiaoBean();
            DB.DBbase dbbase = new DB.DBbase();
            string sql = "";

       /**     //计算比例用选择阶段的总重量
            string zhongliangInPeroid_sql = "select chuku_zhongliang from chukuguanli where pinming = '" + bean.Selectresult[i, 1] + "' and chuku_zhiban = '" + bean.Selectresult[i, 0] + "'";

            float zhongliangInPeroid = 0;

            if (dbbase.ExecuteSQL(zhongliang_sql.Trim()))
            {
                zhongliangInPeroid = float.Parse(dbbase.rows[0, 0].ToString());

            }
        * **/

            for (int i = 0; i < bean.Resultrow; i++)
            {
                //出库表单重
                string zhongliang_sql = "select chuku_no,chuku_zhongliang from chukuguanli where chuku_pinming = '" + bean.Selectresult[i, 0] + "' and chuku_yanshou between '"+startDay+"' and '"+endDay+"'";

                float chuku_zhongliang = 0;
                string chuku_no = "";
                double plusValue = 0;
                double lastValue = 0;
                int currentRow = 0;
                if (dbbase.ExecuteSQL(zhongliang_sql.Trim()))
                {
                    for (int j = 0; j < dbbase.Row_Number; j++)
                    {
                        chuku_no = dbbase.rows[j, 0].ToString();
                        chuku_zhongliang = float.Parse(dbbase.rows[j, 1].ToString());
                       currentRow = dbbase.Row_Number - 1;
                       if (j == currentRow && j != 0)
                        {
                            lastValue = Math.Round((double.Parse(bean.Selectresult[i, 3].ToString()) - plusValue),3);
                            sql = "update  chukuguanli set chuku_zhongliang = '" + Convert.ToString(Math.Round((chuku_zhongliang - lastValue),3)) + "' where chuku_no = '" + chuku_no + "'";
                        }
                        else
                        {
                            sql = "update  chukuguanli set chuku_zhongliang = '" + Convert.ToString(chuku_zhongliang - (Math.Round(chuku_zhongliang / float.Parse(bean.Selectresult[i, 2].ToString()) * float.Parse(bean.Selectresult[i, 3].ToString()), 0))) + "' where chuku_no = '" + chuku_no + "'";
                        }
                        
                        if (!dbbase.ExecuteSQL(sql.Trim()))
                        {
                            return false;
                        }
                        plusValue += Math.Round(chuku_zhongliang / float.Parse(bean.Selectresult[i, 2].ToString()) * float.Parse(bean.Selectresult[i, 3].ToString()),0);
                         
                        
                    }
                    


                }

              
            }

            if (dbbase.command())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
